//
//  UIView+Gesture.m
//  HYSmileiOS
//
//  Created by SMC-MAC on 16/5/16.
//  Copyright © 2016年 heyou. All rights reserved.
//

#import "UIView+Gesture.h"
#import <objc/runtime.h>

#define kUIViewSingleTapKey @"kUIViewSingleTapKey"
#define kUIViewDoubleTapKey @"kUIViewDoubleTapKey"

#define kSingleTapObjectKey @"kSingleTapObjectKey"
#define kDoubleTapObjectKey @"kDoubleTapObjectKey"

@implementation UIView (Gesture)
/**
 *  通过运行时给UIView添加属性
 *
 *  @return singleTap属性
 */
- (UITapGestureRecognizer*)singleTap {
    UITapGestureRecognizer* _tap;
    
    _tap = (UITapGestureRecognizer*)objc_getAssociatedObject(self, kSingleTapObjectKey);
    
    return _tap;
}

- (void)setSingleTap:(UITapGestureRecognizer*)tap {
    objc_setAssociatedObject(self, kSingleTapObjectKey, tap, OBJC_ASSOCIATION_ASSIGN);
}


/**
 *  通过运行时给UIView添加属性
 *
 *  @return singleTap属性
 */
- (UITapGestureRecognizer*)doubleTap {
    UITapGestureRecognizer* _tap;
    
    _tap = (UITapGestureRecognizer*)objc_getAssociatedObject(self, kDoubleTapObjectKey);
    
    return _tap;
}

- (void)setDoubleTap:(UITapGestureRecognizer*)tap {
    objc_setAssociatedObject(self, kDoubleTapObjectKey, tap, OBJC_ASSOCIATION_ASSIGN);
}

/**
 *  给UIView添加手势处理Block
 *
 *  @param action block代码
 */
- (void) handleSingleTapWithBlock:(HandleGestureBlock)action {
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, kUIViewSingleTapKey, action, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    if (self.singleTap == nil) {
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        singleTap.numberOfTapsRequired = 1;
        singleTap.cancelsTouchesInView = YES;
        [self addGestureRecognizer:singleTap];
        self.singleTap = singleTap;
    }
    
    if (self.doubleTap) {
        [self.singleTap requireGestureRecognizerToFail:self.doubleTap];
    }
}

/**
 *  给UIView添加手势处理Block
 *
 *  @param action block代码
 */
- (void) handleDoubleTapWithBlock:(HandleGestureBlock)action {
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, kUIViewDoubleTapKey, action, OBJC_ASSOCIATION_COPY_NONATOMIC);
    if (self.doubleTap == nil) {
        UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.cancelsTouchesInView = YES;
        [self addGestureRecognizer:doubleTap];
        self.doubleTap = doubleTap;
    }
    
    if (self.singleTap) {
        [self.singleTap requireGestureRecognizerToFail:self.doubleTap];
    }
}



/**
 *  点击处理事件
 */
-(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer {
    HandleGestureBlock block;
    if (gestureRecognizer == self.singleTap) {
        block = (HandleGestureBlock)objc_getAssociatedObject(self, kUIViewSingleTapKey);
    } else if (gestureRecognizer == self.doubleTap) {
        block = (HandleGestureBlock)objc_getAssociatedObject(self, kUIViewDoubleTapKey);
    }
    
    if (block) {
        block();
    }
}

@end
